//
//  JJDetailInformationController.m
//  DemoPro
//
//  Created by vpjacob on 2017/5/21.
//  Copyright © 2017年 vpjacob. All rights reserved.
//

#import "JJDetailInformationController.h"
#import "JJRecorderController.h"
#import <UIImageView+WebCache.h>
#import <LLSimpleCamera.h>

@interface JJDetailInformationController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UITextField *Ids;
@property (weak, nonatomic) IBOutlet UITextField *phonenum;
@property (weak, nonatomic) IBOutlet UITextField *createTime;
@property (weak, nonatomic) IBOutlet UITextField *titles;
@property (weak, nonatomic) IBOutlet UITextField *isDel;
@property (nonatomic, strong)LLSimpleCamera *camera;

@end

@implementation JJDetailInformationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"详细界面修改";
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.iconImage addGestureRecognizer:tap];
    [self setupTextFiled];
}

- (void)setupTextFiled{
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString: _model.memberHeading] placeholderImage:[UIImage imageNamed:@"guide1"]];
    self.Ids.text = [NSString stringWithFormat:@"%zd",self.model.Ids];
    self.phonenum.text = [NSString stringWithFormat:@"%zd",self.model.phonenum];
    self.createTime.text = self.model.createTime;
    self.titles.text = self.model.title;
    
}

- (void)addChangeIconAction{
    JJ_WS(weakSelf);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //按钮：从相册选择，类型：UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
        //获取方式1：通过相册（呈现全部相册），UIImagePickerControllerSourceTypePhotoLibrary
        //获取方式2，通过相机，UIImagePickerControllerSourceTypeCamera
        //获取方法3，通过相册（呈现全部图片），UIImagePickerControllerSourceTypeSavedPhotosAlbum
        PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //允许编辑，即放大裁剪
        PickerImage.allowsEditing = YES;
        //自代理
        PickerImage.delegate = self;
        //页面跳转
        [weakSelf presentViewController:PickerImage animated:YES completion:nil];
    }]];
    
    //按钮：拍照，类型：UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        JJRecorderController * vc = [JJRecorderController new];
        vc.resizeImage = ^(UIImage *newImage) {
            weakSelf.iconImage.image = newImage;
        };
        [weakSelf presentViewController:vc animated:YES completion:nil];

    }]];
    //按钮：取消，类型：UIAlertActionStyleCancel
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//        //获取方式:通过相机
//        PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
//        PickerImage.allowsEditing = YES;
//        PickerImage.delegate = self;
//        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)tapAction{
    [self addChangeIconAction];
}

//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    self.iconImage.image = newPhoto;
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
